<script setup lang="ts">
import hljs from 'highlight.js/lib/core';
import { computed, onMounted } from 'vue';
import { NConfigProvider, darkTheme } from 'naive-ui';
import type { WatermarkProps } from 'naive-ui';
import { useAppStore } from './store/modules/app';
import { useThemeStore } from './store/modules/theme';
import { naiveDateLocales, naiveLocales } from './locales/naive';
import { useRouterPush } from './hooks/common';

defineOptions({
    name: 'App'
});

const appStore = useAppStore();
const themeStore = useThemeStore();
const routerPush = useRouterPush();

const naiveDarkTheme = computed(() => (themeStore.darkMode ? darkTheme : undefined));

const naiveLocale = computed(() => {
    return naiveLocales[appStore.locale];
});

const naiveDateLocale = computed(() => {
    return naiveDateLocales[appStore.locale];
});

const watermarkProps = computed<WatermarkProps>(() => {
    return {
        content: themeStore.watermark?.text || 'SoybeanAdmin',
        cross: true,
        fullscreen: true,
        fontSize: 16,
        lineHeight: 16,
        width: 384,
        height: 384,
        xOffset: 12,
        yOffset: 60,
        rotate: -15,
        zIndex: 9999
    };
});

hljs.registerLanguage('naive-log', () => ({
    contains: [
        {
            className: 'number',
            begin: /\d{4}-\d{2}-\d{2}|\d{2}:\d{2}:\d{2}|\d+/
        },
        {
            className: 'string',
            begin: /".*?"/
        },
        {
            className: 'keyword',
            begin: /\b(ERROR|WARN|INFO|DEBUG|TRACE|FATAL)\b/
        },
        {
            className: 'comment',
            begin: /#.*$/
        }
    ]
}));

onMounted(() => {
    routerPush.routerPushByKey('home');
});
</script>

<template>
    <NConfigProvider
        :theme="naiveDarkTheme"
        :theme-overrides="themeStore.naiveTheme"
        :locale="naiveLocale"
        :date-locale="naiveDateLocale"
        :hljs="hljs"
        class="h-full"
    >
        <AppProvider>
            <RouterView class="bg-layout" />
            <NWatermark v-if="themeStore.watermark?.visible" v-bind="watermarkProps" />
        </AppProvider>
    </NConfigProvider>
</template>

<style scoped></style>
